package com.iking.housekeeping.service.servicerecord;

import java.util.List;

import com.iking.housekeeping.entity.elderly.Elderly;
import com.iking.housekeeping.service.elderly.ElderlyService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.iking.housekeeping.dao.changeservice.ChangeServiceMapper;
import com.iking.housekeeping.dao.servicerecord.ServiceRecordMapper;
import com.iking.housekeeping.entity.changeservice.ChangeService;
import com.iking.housekeeping.entity.servicerecord.ServiceRecord;
import com.iking.housekeeping.entity.servicestaff.ServiceStaff;
import com.iking.housekeeping.httpbean.servicerecord.ReqServiceRecord;
import com.iking.housekeeping.httpbean.servicerecord.ReqStatisticsBean;
import com.iking.housekeeping.util.UUIDUtil;
import com.iking.housekeeping.util.token.WebTokenUtil;
import com.iking.housekeeping.httpbean.servicerecord.DetilServiceRecord;
import com.iking.housekeeping.httpbean.servicerecord.HttpServiceRecord;
import com.iking.housekeeping.httpbean.servicerecord.HttpVisitedRecord;
import com.iking.housekeeping.httpbean.servicerecord.SelectServiceRecord;
import com.iking.housekeeping.service.servicestaff.ServiceStaffService;

/**
 * 服务记录
 * 
 * @author: gf
 * @date: 2018年4月22日 下午7:17:58
 */
@Service
public class ServiceRecordService {
	
	/**
	 * 服务记录操作
	 */
	@Autowired
	private ServiceRecordMapper serviceRecordMapper;	
	
	@Autowired
	private ServiceStaffService serviceStaffService;
	
	@Autowired
	private ChangeServiceMapper changeServiceMapper;

	@Autowired
	private ElderlyService elderlyService;
	/**
	 * 	服务记录添加
	 *  @return int
	 *  @author gf
	 */
	public int insert(ServiceRecord req) {
		return serviceRecordMapper.insertSelective(req);
	}
	
	/**
	 * 	批量删除
	 *  @param list
	 *  @return int
	 *  @author gf
	 */
	public int deleteBatch(List<String> list) {
		return serviceRecordMapper.deleteBatch(list);
	}


	/**
	 * 结束服务
	 *
	 * @param req
	 * @return int
	 * @author gf
	 */
	public int endServiceRecord(ServiceRecord req, HttpServletRequest request) {
		int result=0;
		ServiceRecord serviceRecord = serviceRecordMapper.selectByPrimaryKey(req.getfId());
		if ("47b77b336c2e498b8a95a843e57f785d".equals(req.getfIsChangeType())) {
			req.setfServiceStaff(serviceRecord.getfServiceStaff());
			req.setfElderly(serviceRecord.getfElderly());
			req.setfServiceType(serviceRecord.getfServiceType());
			result=changeServiceMapper.insert(new ChangeService(req));
		}
		if (!serviceRecord.getfState().equals(req.getfState())) {
			// 结束当前服务
			result=serviceRecordMapper.updateByPrimaryKeySelective(req);
			// 老人服务次数+1,剩余服务次数-1
			Elderly elderly = elderlyService.selectByPrimaryKey(serviceRecord.getfElderly());
			elderly.setfSurplusTimes(elderly.getfSurplusTimes() - 1);
			elderly.setfServiceTimes(elderly.getfServiceTimes() + 1);
			elderlyService.update(elderly);
			// 服务人员服务次数+1
			ServiceStaff serviceStaff = serviceStaffService.selectByPrimaryKey(serviceRecord.getfServiceStaff());
			serviceStaff.setfServiceTimes(serviceStaff.getfServiceTimes() + 1);
			serviceStaffService.update(serviceStaff);
		}
		 if (req.getfReServation() != null) {
			ReqServiceRecord reqServiceRecord = new ReqServiceRecord();
			reqServiceRecord.setElderlyId(serviceRecord.getfElderly());
			reqServiceRecord.setTime(req.getfReServation());
			 result=addServiceRecord(reqServiceRecord, request);
		}
		return result;
	}


	/**
	 * 	查询所有分页
	 *  @param pageParam
	 *  @return Page<ServiceRecord> 
	 *  @author gf
	 */
	public Page<HttpServiceRecord> selectByPage(SelectServiceRecord selectServiceRecord){
		//设置当前页和分页大小
		PageHelper.startPage(selectServiceRecord.getPageCurrent(), selectServiceRecord.getPageSize());
		//查询所有
		return serviceRecordMapper.selectByPage(selectServiceRecord);
	}
	
	/**
	 * 	通过老人id查询服务记录
	 *  @param fElderlyId
	 *  @return List<ServiceRecord>
	 *  @author gf
	 */
	public List<ServiceRecord> selectByElderId(String fElderly){
		return serviceRecordMapper.selectByElderlyId(fElderly);
	}
	
	/**
	 * 	通过服务人员Id查询服务记录
	 *  @param fServiceStaff
	 *  @return List<ServiceRecord>
	 *  @author gf
	 */
	public List<ServiceRecord> selectByStaffId(String fServiceStaff){
		return serviceRecordMapper.selectByStaffId(fServiceStaff);
	}
	
	/**
	 * 	回访信息展示
	 *  @return List<HttpVisitedRecord>
	 *  @author gf
	 */
	public List<HttpVisitedRecord> visitedRecord(HttpVisitedRecord httpVisitedRecord) {
		return serviceRecordMapper.visitedRecord(httpVisitedRecord);
	}


	/**
	 * 修改服务
	 * @param req
	 * @param request
	 */
	public int updateServiceRecord(ServiceRecord req, HttpServletRequest request) {
		//如果评价内容不为空，则设置状态为已评价
		if(req.getfSatisfaction() != null && !req.getfSatisfaction().isEmpty()) {
			req.setfState("a2b76e7d3580470fb3d49b89ff481f0d");
		}
		return serviceRecordMapper.updateByPrimaryKeySelective(req);
	}

	/**
	 * 添加
	 * @Description:
	 * @author:秦
	 * @param reqServiceRecord
	 * @return
	 */
	public int addServiceRecord(ReqServiceRecord reqServiceRecord,HttpServletRequest request) {
		String serviceStaffId = WebTokenUtil.getUserIdByRequest(request );
		reqServiceRecord.setfStrffId(serviceStaffId);
		ServiceRecord serviceRecord = serviceRecordMapper.selectServiceRecord(reqServiceRecord);
		Elderly elderly = elderlyService.selectByPrimaryKey(reqServiceRecord.getElderlyId());
		if(elderly.getfSurplusTimes()< 1){
			return 3;
		}
		if(serviceRecord == null) {
			String fStatus = "a3eec264faeb4d95843ce0bd6268a162";
			ServiceStaff serviceStaff = serviceStaffService.selectByPrimaryKey(serviceStaffId);
			serviceRecord = new ServiceRecord(reqServiceRecord,0,0.0,UUIDUtil.getUUID(),fStatus);
			serviceRecord.setfServiceType(serviceStaff.getfServiceType());
			return serviceRecordMapper.insertSelective(serviceRecord);
		}
		return serviceRecordMapper.updateByPrimaryKeySelective(new ServiceRecord(reqServiceRecord.getTime(),serviceRecord.getfId()));
	}
	
	/**
	 * 开始服务
	 * @author 秦
	 * @param sRecord
	 * @return
	 */
	public int updateByPrimaryKeySelective(ServiceRecord sRecord) {
		return serviceRecordMapper.updateByPrimaryKeySelectiveOld(sRecord);
	}
	
	public DetilServiceRecord selectServiceRecord(String fId) {
		return serviceRecordMapper.selectServiceRecords(fId);
	}
	/**
	 * 
	 * @Description:查询总订单数
	 * @author:CGZ
	 * @param reqStatisticsBean 
	 * @return
	 */
	public Integer selectTotal(ReqStatisticsBean reqStatisticsBean) {
		return serviceRecordMapper.selectTotal(reqStatisticsBean);
	}
	/**
	 * 
	 * @Description:查询未完成订单
	 * @author:CGZ
	 * @param reqStatisticsBean 
	 * @return
	 */
	public Integer selectUndone(ReqStatisticsBean reqStatisticsBean) {
		return serviceRecordMapper.selectUndone(reqStatisticsBean);
	}
	/**
	 * 
	 * @Description:查询已完成订单
	 * @author:CGZ
	 * @param reqStatisticsBean 
	 * @return
	 */
	public Integer selectCompleted(ReqStatisticsBean reqStatisticsBean) {
		return serviceRecordMapper.selectCompleted(reqStatisticsBean);
	}

	/**
	 * 判断老人是否可以继续预约
	 *
	 * @param serviceRecord
	 * @return
	 */
	public Boolean judge(ServiceRecord serviceRecord) {
		// 没有预约时间直接过
		if (serviceRecord.getfReServation() == null) {
			return true;
		}
		// 获取服务详情
		ServiceRecord record = serviceRecordMapper.selectByPrimaryKey(serviceRecord.getfId());
		// 获取老人信息
		Elderly elderly = elderlyService.selectByPrimaryKey(record.getfElderly());
		// 剩余一次只够结束当前任务，不能继续预约
		if (elderly.getfSurplusTimes() <= 1) {
			return false;
		}
		return true;
	}

	/**
	 * 判断是否可以开始服务
	 *
	 * @param updateElderly
	 * @return
	 */
	public Boolean judgeBegin(ServiceRecord updateElderly) {
		// 获取服务详情
		ServiceRecord record = serviceRecordMapper.selectByPrimaryKey(updateElderly.getfId());
		// 获取老人信息
		Elderly elderly = elderlyService.selectByPrimaryKey(record.getfElderly());
		if (elderly == null) {
			return false;
		}
		return true;
	}
	/**
	 *根据服务记录id查询正在进行的服务记录
	 *
	 * @return List<String> 记录id
	 */
	public List<String> selectState(List<String> list){
		return serviceRecordMapper.selectState(list);
	}
}
